<?php

class IsCart {

  function IsCart($inCMS = false)
  {
    $this->inCMS = $inCMS;
  }
  

  function initSmallCart (&$shop,$params) 
  {
     if ($this->inCMS)
        $cart  = $GLOBALS['env']->getSession('s_cart');
     else
        $cart  = $shop->getSessionVar('s_cart');
     $count = count($cart);
     $shop->setBlockVar('number',$count);
     $num   = 0;
     $price = 0;
     for ($i=0;$i<$count;$i++){
       $num    += $cart[$i]['quantity'];
       $price  += $cart[$i]['price']*$cart[$i]['quantity'];
     }
     $shop->setBlockVar('totalNumber',$num);
     $shop->setBlockVar('totalPrice',$price);
     if ($count) {
       return true;
     }
  }

  function numPosCart (&$shop,$params = ''){
    if (!$this->cart){
        if ($this->inCMS)
            $this->cart  = $GLOBALS['env']->getSession('s_cart');
        else
            $this->cart  = $shop->getSessionVar('s_cart');
        $this->cartcount = 0;
    }
    return count($this->cart);
  }

  function cartLink (&$shop,$params) {
    $shop->out('<a href="'.$shop->getVar('SCRIPT_NAME').'?tr='.$shop->getVar('TEMPLATE_ROOT').'&t='.$shop->cfgTpl['cart'].'" target="'.$shop->cfgTplFrame['cart'].'">'.$params[0].'</a>');
  }

  function nextCartArticle (&$shop,$params = '') {
    if (!$this->cart){
        if ($GLOBALS['env'])
            $this->cart = $GLOBALS['env']->getSession('s_cart');
        else
            $this->cart = $shop->getSessionVar('s_cart');
        $this->cartcount = (int) 0;
    }
    //print "test"; print_r($_SESSION['shop_session']); print "test";
    if ($this->cart[$this->cartcount]){
      $shop->removeBlockVars();
      while (list($name,$value) = each($this->cart[$this->cartcount])){
        $shop->setBlockVar($name,$value);
      }
      $shop->setBlockVar('subtotal',($this->cart[$this->cartcount]['price'] * $this->cart[$this->cartcount]['quantity']));
      $shop->setBlockVar('pos',$this->cartcount);
      $this->cartcount++;
      return true;
    }
  }

  function totalTax (&$shop,$params = '') {
    if (!$this->totaltax){
     for ($i=0;$i<count($this->cart);$i++){
       $this->totaltax += $this->cart[$i]['taxamount']*$this->cart[$i]['quantity'];
     }
    }
    return $this->totaltax;
  }

  // p1 : string ( blabla $name bla $price )
  function cartConfiguratorArticles (&$shop,$params = '') {
    foreach ($params As $p){
      $str .= $p;
    }
    if ($this->cart[$this->cartcount-1]['cnfart']){
       foreach ($this->cart[$this->cartcount-1]['cnfart'] As $cnfart){
         $html = $str;
         while (preg_match('/(\$[\w]+)/',$html,$matches)){
            preg_match('/\$([\w_]+)/',$matches[1],$m);
            if ($m[1] == 'price') {
              $replacement = $shop->call('currency','currencyStr',$cnfart[$m[1]]);
            } else {
              $replacement = $cnfart[$m[1]];
            }
            $html = str_replace($matches[0],$replacement,$html);
         }
         $shop->out($html);
       } # foreach
    }
  }

  function totalPrice (&$shop,$params = '') {
    if (!$this->totalPrice){
     for ($i=0;$i<count($this->cart);$i++){
       $this->totalPrice += $this->cart[$i]['quantity']*$this->cart[$i]['price'];
     }
    }
    return $this->totalPrice;
  }

  function totalWeight (&$shop,$params = '') {
    if (!$this->totalWeight){
     for ($i=0;$i<count($this->cart);$i++){
       $this->totalWeight += $this->cart[$i]['quantity']*$this->cart[$i]['kg'];
     }
    }
    return $this->totalWeight;
  }

  function totalPackets (&$shop,$params = '') {
    if (!$this->totalPackets){
     $count = 0;
     for ($i=0;$i<count($this->cart);$i++){
       if ($this->cart[$i]['packet']){
         $count++;
       }
     }
     if (count($this->cart) > $count) $this->totalPackets++;
    }
    return $this->totalPackets;
  }

  function deleteArticleUrl(&$shop) {
    return $shop->getVar('SCRIPT_NAME').'?doc_id='.$GLOBALS['doc_id'].
            '&cartaction=del&cid='.(int)$shop->getVar('pos');  
  }

  function deleteArticleLink (&$shop,$params) {
    if ($this->inCMS)
        print '<a href="'.$this->deleteArticleUrl($shop).'">'.$params[0].'</a>';
    else
        $shop->out('<a href="'.$shop->getVar('SCRIPT_NAME').'?a=da&tr='.$shop->getVar('TEMPLATE_ROOT').'&t='.$shop->cfgTpl['cart'].'&cid='.(int)$shop->getVar('pos').'" target="'.$shop->cfgTplFrame['cart'].'">'.$params[0].'</a>');
  }

  function deleteAllArticlesUrl (&$shop) {
    return $shop->getVar('SCRIPT_NAME').'?doc_id='.$GLOBALS['doc_id'].'&cartaction=delall';
  }

  function deleteAllArticlesLink (&$shop,$params) {
    if ($this->inCMS)
        print '<a href="'.$this->deleteAllArticlesUrl($shop).'">'.$params[0].'</a>';
    else
        $shop->out('<a href="'.$shop->getVar('SCRIPT_NAME').'?a=daa&tr='.$shop->getVar('TEMPLATE_ROOT').'&t='.$shop->cfgTpl['cart'].'" target="'.$shop->cfgTplFrame['cart'].'">'.$params[0].'</a>');
  }

  function beginCartForm (&$shop,$params='') {
    if ($this->inCMS)
        print '<form action="'.$shop->getVar('SCRIPT_NAME').'"><input type="hidden" name="cartaction" value="update"/><input type="hidden" name="doc_id" value="'.$GLOBALS['doc_id'].'"/>'; 
    else
        $shop->out('<form action="'.$shop->getVar('SCRIPT_NAME').'" target="'.$shop->cfgTplFrame['cart'].'"><input type="hidden" name="tr" value="'.$shop->getVar('TEMPLATE_ROOT').'"><input type="hidden" name="t" value="'.$shop->cfgTpl['cart'].'"><input type="hidden" name="a" value="uc">');
  }

  function cartArticleFieldName(&$shop,$params = '') {
    $shop->out('ca['.(int)($this->cartcount-1).']');
  }

  function updateCartSubmit (&$shop,$params) {
    $shop->out('<input type="'.$params[0].'" ');
    if ($params[0] == 'image') {
      $shop->out('src="'.$params[1].'" '.$params[2].'>');
    } else {
      $shop->out('value="'.$params[1].'" '.$params[2].'>');
    }
  }

  function endCartForm (&$shop,$params='') {
    $shop->out('</form>');
  }

  function orderLink (&$shop,$params = '') {
    $shop->out('<a href="'.$shop->getVar('SCRIPT_NAME').'?tr='.$shop->getVar('TEMPLATE_ROOT').'&t='.$shop->cfgTpl['order_start'].'" target="'.$shop->cfgTplFrame['order_start'].'">'.$params[0].'</a>');
  }

}

?>